﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace webnew.Systems
{
    public partial class S_deptList : ListBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _ListTbName = "Sys_Dept";       //执行删除语句的数据库表名
            _EditSrc = "S_deptEdit.aspx";  //编辑页路径
            _DelType = DeleteType.FalseDel;//逻辑删除
            _Grdview = grdView;//主列表控件                  
            btnExcelVisble = false; //设置导出不可见


            if (!IsPostBack)
            {
                SetTitle(true, "后台管理 >> 部门管理");
                UcCompany1.show(true,"ALL");
                //UcCompany2.show(true, "HR");
                webnew.Common.CommonHelper.DataDdlBind(ddlDeptLevel, new BLL.Sys_SystemType().GetconList("系统部门级别"), "TypeName", "TypeName");
                ddlDeptLevel.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));
                AspNetPager1_PageChanged(null, null);
            }
        }

        /// <summary>
        /// 重写删除事件,使删除后刷新页面;
        /// </summary>
        protected override void _Del()
        {

            //根据选中的部门ID,判断是否能删除
            if (new BLL.Sys_Dept().isCanDel(GetSelecetedIDs(grdView)) == false)
            {
                ShowMsg = "部门使用中,不能删除!";
                return;
            }
            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            try
            {
                base._Del();
            }
            catch (Exception e)
            {
                error = e.ToString();
            }
            finally
            {
                //添加一条系统日志
                new BLL.Sys_Log().AddSysLog(error, "删除", modelLog, "系统部门", "部门，部门名称为" + GetNames("deptname", _ListTbName));
            }
            AspNetPager1_PageChanged(null, null);
        }

        /// <summary>
        /// 检索按钮事件
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {

            AspNetPager1_PageChanged(null, null);

        }


        /// <summary>
        /// 分页控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //分页的查询表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            string strFeild = "a.*,b.compname as oacompname,(select deptname from Sys_Dept where id= a.ParentID) as ParentName";
            strtable.Append(" Sys_Dept a left join Sys_company b on a.compid=b.id  ");

            //分页的查询的条件
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();
            _conditon.Append(" b.isdel=0  ");
            _conditon.Append(" and a.isdel=0 and a.deptname like '%" + txtDeptName.Text.Trim().Replace("'", "") + "%'");
            if (ddlDeptLevel.SelectedValue != Guid.Empty.ToString()) _conditon.Append(" and a.DeptLevel='" + ddlDeptLevel.SelectedValue + "' ");
            if (UcCompany1.SelectedValue != Guid.Empty.ToString()) _conditon.Append(" and a.CompId='" + UcCompany1.SelectedValue + "' ");
            AspNetPager1.RecordCount = new BLL.Sys_Dept().GetCount(strtable.ToString(), _conditon.ToString());
            webnew.Common.CommonHelper.DataGrdBind(grdView, new BLL.Sys_Dept().GetPageList(strFeild, _conditon.ToString(), grdView.PageSize, AspNetPager1.CurrentPageIndex, strtable.ToString(), "a.DeptLevel,b.compname,a.sort,a.deptname,a.id", true));
        }

        /// <summary>
        /// 显示条数下拉框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdView.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            AspNetPager1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            AspNetPager1_PageChanged(null, null);
        }


        protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //鼠标经过变色
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "GridViewMouseover(this)");
                e.Row.Attributes.Add("onmouseout", "GridViewMouseout(this)");
                e.Row.Attributes.Add("onclick", "GridViewonclick(this)");
                e.Row.Cells[4].Text = e.Row.Cells[4].Text == Guid.Empty.ToString() ? "" : e.Row.Cells[4].Text;
            }
        }





    }
}
